package cc.bailongma.me;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.os.Handler;
import android.view.KeyEvent;
import android.widget.TextView;

public class WelcomeActivity extends Activity {
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_welcome);
		setPackageInfo();
		goNextPage();
	}

	private void goNextPage() {
		Handler h = new Handler();
		h.postDelayed(new Runnable() {
			@Override
			public void run() {
				startNextActivity();
			}
		}, 1000);
	}

	private void startNextActivity() {
		SharedPreferences sp = getSharedPreferences("config",
				Context.MODE_PRIVATE);
		boolean flag = sp.getBoolean("isGrid", true);
		if (flag) {
			startAty(GuideActivity.class);
			Editor et = sp.edit();
			et.putBoolean("isGrid", false);
			et.commit();
		} else {
			startAty(LoginActivity.class);
		}
		finish();
	}

	private void startAty(Class<?> clazz) {
		Intent intent = new Intent(this, clazz);
		startActivity(intent);
	}

	private void setPackageInfo() {
		PackageManager pm = getPackageManager();
		try {
			PackageInfo pinfo = pm.getPackageInfo(getPackageName(), 0);
			TextView tv = (TextView) findViewById(R.id.tv_1);
			tv.setText(pinfo.versionName);
		} catch (Exception e) {
		}
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			return false;
		}
		return true;
	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
	}

}
